import {Component, ElementRef, EventEmitter, Injector, Input, OnInit, Output, TemplateRef, ViewEncapsulation} from '@angular/core';
import {Cell} from "../table.model";
import {AppService} from "../../../../app.service";
import {animates} from "../../../../support/animations";
import {TableComponent} from "../table.component";
import {generateFieldId} from "../../../../support/generateFieldId";
import {extend} from "../../../../support/extend";
import {FieldRef, FieldSource} from "../../field/field.definition";

@Component({
  selector: 'ss-platform-table-cell',
  templateUrl: './cell.component.html',
  styleUrls: ['./cell.component.scss'],
  host: {class: "ss-platform-table-cell", "[style.left.px]": "cell.left||0"},
  animations: animates,
  encapsulation: ViewEncapsulation.None
})
export class CellComponent implements OnInit {

  // 是否列头
  @Input("ss-is-header")
  isHeader: boolean = false;
  @Input("ss-index")
  index: number = 0;
  @Input("ss-cell")
  cell: Cell;
  @Input("ss-item")
  item: any = {};
  @Output()
  changeTitle: EventEmitter<any> = new EventEmitter<any>();
  @Output()
  addCell: EventEmitter<any> = new EventEmitter<any>();

  isSet: boolean = false;

  @Input("templateRef")
  templateRef: TemplateRef<any>;
  // Cell Element
  element: HTMLElement;

  constructor(private elementRef: ElementRef, private tableComponent: TableComponent) {
  }

  ngOnInit(): void {
    let self = this;
    self.element = this.elementRef.nativeElement;
  }

  onSetTypeValue(type: string): void {
    this.cell.field.type = type;
    let iconElement = this.element.querySelector(".icon") as HTMLElement;
    iconElement.classList.add("slide");
    setTimeout(function () {
      iconElement.classList.remove("slide");
    }, 400)
  }

  /**
   * 自动选中
   */
  cellEditInit(): void {
    let inputElement = this.elementRef.nativeElement.querySelector(".ss-platform-table-cell-edit input") as HTMLInputElement;
    if (inputElement) {
      inputElement.select();
    }
  }
}
